package com.cinsea.crm.ext;

import com.cinsea.common.ActionType;
import com.cinsea.common.BasePX;
import com.cinsea.common.Env;
import com.cinsea.common.JdbcDao;
import com.cinsea.util.DateUtil;
import com.cinsea.util.StringUtil;
import java.util.List;
import java.util.Map;

public class CrmPX extends BasePX{
    @Override
    public void after(Map map) {
        String dowId = StringUtil.null2String(map.get("dowid"));//目录或流程ID
        String processId = StringUtil.null2String(map.get("processid"));
        String tabName = "";
        if(dowId.equals("402881e43d1436e5013d14668fca0004")){//客户管理
            tabName="ut_contact";
        }
        if(dowId.equals("402881e43d3a2b27013d42dcafa0375e")){//商机管理
            tabName="ut_opportunities";
        }
        if(dowId.equals("402881e43d1436e5013d15607f6c00dd")){//商机联系管理
            tabName="ut_contactlist";
        }
        String sql = "SELECT crmid from "+tabName+" where processid=?";
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        List list = jdbcDao.queryForList(sql,new Object[]{processId});
        String crmid = "";
        if(list.size()>0){
            Map cust = (Map)list.get(0);
            crmid = StringUtil.null2String(cust.get("crmid"));//操作人的id
            if(StringUtil.isEmpty(crmid.trim())){
                return;
            }
        }
        String currentUser = Env.getRemoteUser().getId();
        sql ="update ut_contactlist set xclxrq='' where managerid=? and crmid=? and processid!=?" ;
        jdbcDao.update(sql,new Object[]{currentUser,crmid,processId});
        sql = "delete from cs_log where objid=? and logtype=85 and submitor !=?";//删除日志记录
        jdbcDao.update(sql,new Object[]{crmid,currentUser});
        //更新最后修改日期字段
        String currentdatetime = DateUtil.getCurDateTime();
        if (tabName.equals("ut_contactlist")) {
            sql = "update ut_customerinfo set modifydatetime = ?,zhlxrq=? where processid=?";
            jdbcDao.update(sql, new Object[]{currentdatetime, DateUtil.getCurrentDate(), crmid});
        } else {
            sql = "update ut_customerinfo set modifydatetime = ? where processid=?";
            jdbcDao.update(sql, new Object[]{currentdatetime, crmid});
        }
    }
}
